import Vue from 'vue'
import { login, logout } from '@/api/login'
import {
  ACCESS_TOKEN,
  USER_NAME,
  USER_INFO,
  UI_CACHE_DB_DICT_DATA,
  USER_ID,
  USER_LOGIN_NAME,
  CACHE_INCLUDED_ROUTES
} from '@/store/mutation-types'
import { getLanCode } from '@/lang'
import { welcome } from '@/utils/util'
import { queryPermissionsByUser } from '@/api/api'
import { getAction } from '@/api/manage'
import menus_text from '@/lang/menus'
const user = {
  state: {
    token: '',
    username: '',
    realname: '',
    welcome: '',
    avatar: '',
    // lanCode: getLanCode(),
    permissionList: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { username, realname, welcome }) => {
      state.username = username
      state.realname = realname
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PERMISSIONLIST: (state, permissionList) => {
      state.permissionList = permissionList
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // CAS验证登录
    ValidateLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        getAction('/cas/client/validateLogin', userInfo)
          .then(response => {
            console.log('----cas 登录--------', response)
            if (response.success) {
              const result = response.result
              const userInfo = result.userInfo
              Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
              Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
              Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
              commit('SET_TOKEN', result.token)
              commit('SET_INFO', userInfo)
              commit('SET_NAME', { username: userInfo.username, realname: userInfo.realname, welcome: welcome() })
              commit('SET_AVATAR', userInfo.avatar)
              resolve(response)
            } else {
              resolve(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo)
          .then(response => {
            if (response.code == 200) {
              if (response.data.msgTip == 'user can login') {
                const result = response.data
                Vue.ls.set(USER_ID, result.user.id, 7 * 24 * 60 * 60 * 1000)
                Vue.ls.set(USER_LOGIN_NAME, result.user.loginName, 7 * 24 * 60 * 60 * 1000)
                //前端7天有效期，后端默认1天，只要用户在1天内有访问页面就可以一直续期直到7天结束
                Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
                Vue.ls.set(USER_INFO, result.user, 7 * 24 * 60 * 60 * 1000)
                commit('SET_TOKEN', result.token)
              }
              commit('SET_INFO', userInfo)
              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 获取用户信息
    GetPermissionList({ commit }) {
      return new Promise((resolve, reject) => {
        //let v_token = Vue.ls.get(ACCESS_TOKEN);
        let params = { pNumber: 0, userId: Vue.ls.get(USER_ID) }
        queryPermissionsByUser(params)
          .then(response => {
            const menuData = response
            if (menuData && menuData.length > 0) {
              // console.log(user.state.lanCode)
              let lanCode = getLanCode()
              if (lanCode === 'en') {
                // if (user.state.lanCode === 'en') {
                // console.log(123456);
                // console.log(menuData);

                console.log(menus_text, '123456789')
                for (const key in menuData) {
                  let txt = menuData[key].text
                  var _menu = menus_text[txt]
                  if (_menu) {
                    menuData[key].text = _menu
                  }

                  // console.log(txt);
                  if (menuData[key].children) {
                    for (const itext in menuData[key].children) {
                      let abc = menuData[key].children[itext].text
                      var _children = menus_text[abc]
                      if (_children) {
                        menuData[key].children[itext].text = _children
                      }
                    }
                  }
                }
              }
              commit('SET_PERMISSIONLIST', menuData)
            } else {
              reject('getPermissionList: permissions must be a non-null array !')
            }
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise(resolve => {
        //let logoutToken = state.token;
        commit('SET_TOKEN', '')
        commit('SET_PERMISSIONLIST', [])
        Vue.ls.remove(USER_ID)
        Vue.ls.remove(USER_LOGIN_NAME)
        Vue.ls.remove(USER_INFO)
        Vue.ls.remove(UI_CACHE_DB_DICT_DATA)
        Vue.ls.remove(CACHE_INCLUDED_ROUTES)
        logout()
          .then(() => {
            resolve()
          })
          .catch(() => {
            resolve()
          })
      })
    },
    // 第三方登录
    ThirdLogin({ commit }, token) {
      return new Promise((resolve, reject) => {
        thirdLogin(token)
          .then(response => {
            if (response.code == '200') {
              const result = response.result
              const userInfo = result.userInfo
              Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
              Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
              Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
              commit('SET_TOKEN', result.token)
              commit('SET_INFO', userInfo)
              commit('SET_NAME', { username: userInfo.username, realname: userInfo.realname, welcome: welcome() })
              commit('SET_AVATAR', userInfo.avatar)
              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default user
